/**
 * 
 */
package com.hawkfly.bondingclass;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.wbcs.config.Config;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.jbsf.dao.IDataModal;
import com.wbcs.jbsf.dao.JBSFDaoTemplate;
import com.wbcs.jbsf.util.Wbcs4JBSFUtil;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.component.application.report.configbean.editablereport.AbsJavaEditActionBean;

/**
 * 
 * @author hawkfly
 */
public class SDCMNewsCopy extends AbsJavaEditActionBean
{
    @Override
	public void updateData(ReportRequest rrequest,ReportBean rbean,Map<String,String> mRowData,Map<String,String> mParamValues)
    {
        //执行复制语句
        //复制新闻
        JBSFDaoTemplate daoTemplate = new JBSFDaoTemplate(false);
        String newssql = new StringBuffer("insert into T_NEWS_DETAILS(ID, BT, JT, FBT, GJZ, GY, NRFL, LMFL, SYTP, ZYXX, FBSJ, ordertime, XWLY, XWZT, DJL, SFSY, SFTJ, FBR, BZ, BY1, BY2, XWNR, ZDBM, BSZT, CZR) ")
                                    .append("select ?, BT, JT, FBT, GJZ, GY, NRFL, ?, ?, ?, ?, ?, XWLY, 1, DJL, SFSY, SFTJ, FBR, BZ, BY1, BY2, XWNR, ZDBM, 1, ? ")
                                    .append("from  T_NEWS_DETAILS ")
                                    .append("where ID = ?").toString();
        Object pID = mParamValues.get("pID");
        Object cpID = mParamValues.get("cpID");
        Object LMFL = mRowData.get("LMFL");
        Object CZR = mRowData.get("CZR");
        Object ZDBM = mRowData.get("ZDBM");
        if(ZDBM != null)//推送主站
        {
            newssql = new StringBuffer("insert into T_NEWS_DETAILS(ID, BT, JT, FBT, GJZ, GY, NRFL, LMFL, SYTP, ZYXX, FBSJ, ordertime, XWLY, XWZT, DJL, SFSY, SFTJ, FBR, BZ, BY1, BY2, XWNR, ZDBM, BSZT, CZR) ")
                    .append("select ?, BT, JT, FBT, GJZ, GY, NRFL, ?, ?, ?, ?, ?, XWLY, 1, DJL, SFSY, SFTJ, FBR, BZ, BY1, BY2, XWNR, '1', 1, ? ")
                    .append("from  T_NEWS_DETAILS ")
                    .append("where ID = ?").toString();
        }
        
        if(LMFL == null)LMFL = "";
        java.sql.Timestamp time = null;
        try
        {
           java.util.Date ptime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(mParamValues.get("ptime"));
           time = new java.sql.Timestamp(ptime.getTime());
        }catch(ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Object ID = mRowData.get("ID");
        if(ID == null)return;
        //复制资源
        Object ZYXX = mRowData.get("ZYXX");
        //Object SYTP = mRowData.get("SYTP");
        Connection conn = rrequest.getConnection();
        try{
	        if(ZYXX != null && !"".equals(ZYXX)){//新闻具有图集信息
	            String[] ZYXXS = String.valueOf(ZYXX).split(",");
	            StringBuffer ininner = new StringBuffer();
	            for(int i=0;i<ZYXXS.length;i++)
	            {
	                ininner.append(" ? ,");
	            }
	            String nokh = ininner.substring(0, ininner.lastIndexOf(","));
	            String ininnersql = new StringBuffer(nokh).append(")").toString();
	            String resourcesql = new StringBuffer("insert into T_RESOURCE_INFO(ID, ZYLX, ZYBT, ZYJS, ZYLJ, BZ, BY1,BY2)")
	                                        .append("select SQU_SDCM.nextval,ZYLX, ZYBT, ZYJS, ZYLJ, BZ, BY1, ? from T_RESOURCE_INFO where ID in (").append(ininnersql).toString();
	            String uuid = UUID.randomUUID().toString();
	            JBSFDaoTemplate daoTemplate1 = new JBSFDaoTemplate(true);
	            
	            daoTemplate1.update(resourcesql,Config.getInstance().getDataSource("ds_oracle").getConnection(), uuid, ZYXXS);
	            
	            List<String> lstId = daoTemplate.<List<String>>select("select ID from T_RESOURCE_INFO where BY2=?",rrequest.getConnection(), new IDataModal<List<String>>(){
	                public List<String> loadDatas(ResultSet rs,int ccount,ResultSetMetaData rmd,List<String> dataModal) throws SQLException
	                {
	                    dataModal = new ArrayList<String>();
	                    while(rs.next()){
	                        dataModal.add(rs.getString(1));
	                    }
	                    return dataModal;
	                }},uuid);
	            ZYXX = Wbcs4JBSFUtil.lst2dotStr(lstId);//获取新的资源信息串
	            //更新索引图片
	            /*String sytpidxsql = "SELECT (select idx from (SELECT ROWNUM idx, COLUMN_VALUE FROM TABLE(select jbsfutilspkg.f_split(ZYXX, ',') from  T_NEWS_DETAILS where ID=?)) t where COLUMN_VALUE=SYTP) idx from T_NEWS_DETAILS where ID=?";
	            String sytpsql = "update T_NEWS_DETAILS set(SYTP) = (select ID from T_RESOURCE_INFO where BY2=? and ZYLJ = (select ZYLJ from T_RESOURCE_INFO where ID=?)) where ID = ?";
	            int sytpidx = daoTemplate.<Integer>select(sytpidxsql,rrequest.getConnection(),new IDataModal<Integer>(){
	                public Integer loadDatas(ResultSet rs,int ccount,ResultSetMetaData rmd,Integer dataModal) throws SQLException
	                {
	                    dataModal = 0;
	                    while(rs.next()){
	                        dataModal = rs.getInt(1);
	                    }
	                    return dataModal;
	                }},ID, ID);
	            if(sytpidx <= 0){//未设置索引图片或者索引图片无效的情况
	                SYTP = null;
	            }else{
	                SYTP = lstId.get(sytpidx - 1);
	            }
	            daoTemplate.update(sytpsql, rrequest.getConnection(), uuid, SYTP, pID);*/
	        }
	        daoTemplate.update(newssql,conn,pID, LMFL, cpID, ZYXX, time, time, CZR, ID);
        }catch(Exception e){
        	e.printStackTrace();
        	try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        }
    }

}

